﻿<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Edit.Master" Inherits="System.Web.Mvc.ViewPage<Langben.DAL.ts_lend>" %>

<%@ Import Namespace="Common" %>
<%@ Import Namespace="Models" %>
<asp:Content ID="Content4" ContentPlaceHolderID="CurentPlace" runat="server">
   归还
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
    <fieldset>
        <legend>
            <input class="a2 f2" type="button" value="修改" onclick="save()"/>
            <input class="a2 f2" type="button" onclick="BackList('ts_lend')" value="返回列表" />
        </legend>
        <div class="bigdiv">
            <%: Html.HiddenFor(model => model.Id ) %>     
             <%: Html.HiddenFor(model => model.GoodsDetail)%> 
             <%: Html.HiddenFor(model => model.c_type) %>
             <%: Html.HiddenFor(model => model.i_sign)%>
             <%: Html.HiddenFor(model => model.i_no)%>


            <%: Html.HiddenFor(model => model.dt_dt)%>
            <%: Html.HiddenFor(model => model.c_base)%>
            <%: Html.HiddenFor(model => model.c_id)%>
            <%: Html.HiddenFor(model => model.c_orgno)%>
            <%: Html.HiddenFor(model => model.c_agency)%>
            <%: Html.HiddenFor(model => model.c_keeper)%>
            <%: Html.HiddenFor(model => model.c_clerk)%>
            <%: Html.HiddenFor(model => model.c_comment)%>
 
              <fieldset style="width:850px"> 
            <table style="width:100%"> 
                <tr><td> <%: Html.LabelFor(model => model.i_no) %>：</td>
                <td> <input type="text" id="tbi_no" /> </td>
            <td> <%: Html.LabelFor(model => model.c_orgno) %>：</td>
                <td><input type="text" id="tbc_orgno" /></td>
            <td><%: Html.LabelFor(model => model.dt_dt) %>：</td>
            <td><input type="text" id="tbdt_dt" /></td>
                </tr>
                <tr><td><%: Html.LabelFor(model => model.c_id) %>：</td>
                <td colspan="3"><input type="text" id="tbc_id" /></td>
                <td><%: Html.LabelFor(model => model.c_base) %>：</td>
                <td><input type="text" id="tbc_base" /></td></tr>
                <tr><td> <%: Html.LabelFor(model => model.c_agency) %>：</td>
                <td> <input type="text" id="tbc_agency" /></td>
                <td><%: Html.LabelFor(model => model.c_keeper) %>：</td>
                <td><input type="text" id="tbc_keeper" /></td>
                <td><%: Html.LabelFor(model => model.c_clerk) %>：</td>
                <td><input type="text" id="tbc_clerk" /></td></tr>
                <tr><td> <%: Html.LabelFor(model => model.c_comment) %>：</td>
                <td colspan="5"><input type="text" id="tbc_comment" /></td> </tr>
                </table>  
                 </fieldset>
                 <div style="float:left;"> 
                 <div style="width:880px; height:350px"> 
                  <table id="flexigridData">
                  </table>

                 </div>  
              </div>
        </div>
    </fieldset>
</asp:Content>
<asp:Content ID="Content3" ContentPlaceHolderID="HeadContent" runat="server">
 <script src="<%: Url.Content("~/Scripts/json2.js") %>" type="text/javascript"></script>

    <script type="text/javascript">

        $(function () {
            DoGrid();

            $('#tbi_no').val($('#i_no').val());
            $('#tbdt_dt').val($('#dt_dt').val());
            $('#tbc_base').val($('#c_base').val());
            $('#tbc_id').val($('#c_id').val());
            $('#tbc_orgno').val($('#c_orgno').val());
            $('#tbc_agency').val($('#c_agency').val());
            $('#tbc_keeper').val($('#c_keeper').val());
            $('#tbc_clerk').val($('#c_clerk').val());
            $('#tbc_comment').val($('#c_comment').val());


            $('#tbi_no').attr("disabled", true);
            $('#tbdt_dt').attr("disabled", true);
            $('#tbc_base').attr("disabled", true);
            $('#tbc_id').attr("disabled", true);
            $('#tbc_orgno').attr("disabled", true);
            $('#tbc_agency').attr("disabled", true);
            $('#tbc_keeper').attr("disabled", true);
            $('#tbc_clerk').attr("disabled", true);
            $('#tbc_comment').attr("disabled", true);

            $('#flexigridGoodsData').datagrid({
                // title: '物品目录', //列表的标题
                iconCls: 'icon-site',
                width: 'auto',
                height: 'auto',
                nowrap: false,
                striped: true,
                collapsible: true,
                url: '../ts_goods/GetData', //获取数据的url
                sortName: 'Id',
                sortOrder: 'desc',
                idField: 'Id',

                toolbar: [
                     {
                         text: '选择',
                         iconCls: 'icon-ok',
                         handler: function () {
                             return flexiSelect();
                         }
                     }],
                columns: [[
                      { field: 'i_sign', title: '状态', width: 60 }
                	, { field: 'c_code', title: '编号', width: 80 }
				 	, { field: 'c_desc', title: '物品', width: 80 }
					, { field: 'c_model', title: '规格', width: 80 }
					, { field: 'c_unit', title: '单位', width: 80 }
					, { field: 'c_class', title: '类别', width: 80 }
					, { field: 'c_class1', title: '大类别', width: 52, hidden: true }
					, { field: 'f_pricelast', title: '最后进价', width: 52, hidden: true }
					, { field: 'f_upper', title: '库存上限', width: 60 }
					, { field: 'f_lower', title: '库存下限', width: 60 }
					, { field: 'c_py', title: '拼音', width: 52, hidden: true }
					, { field: 'c_comment', title: '备注', width: 52, hidden: true }
					, { field: 'c_vendor', title: '主供应商', width: 90 }
					, { field: 'c_base', title: '专用仓库', width: 90 }
                ]],
                pagination: true,
                rownumbers: true

            });

        });


        function save() {
            var rows = $('#flexigridData').datagrid('getRows');
            var count = 0;
            var allrow = "";
            for (var i = 0; i < rows.length; i++) { 
                var row = JSON.stringify(rows[i]);
                allrow = allrow + row + "^";
            }
             
            $('#GoodsDetail').val(allrow); 
            $.ajax({
                url: this.action,
                type: "Post",
                data: $(document.forms[0]).serialize(),
                dataType: "json",
                success: function (data) {
                    if ($.messager) {
                        $.messager.defaults.ok = '继续';
                        $.messager.defaults.cancel = '返回';

                        $.messager.confirm('操作提示', data, function (r) {
                            if (!r) {
                                window.location.href = 'javascript:history.go(-1)';
                            }
                        });
                    }

                }
            });


        }



        //“选择”按钮，在其他（与此页面有关联）的页面中，此页面以弹出框的形式出现，选择页面中的数据
        function flexiSelect() {

            var rows = $('#flexigridGoodsData').datagrid('getSelections');
            if (rows.length == 0) {
                $.messager.alert('操作提示', '请选择数据!', 'warning');
                return false;
            }
            var indexs = 0;
            for (var i = 0; i < rows.length; i++) {

                indexs = i;
                $('#flexigridData').datagrid('insertRow', {
                    index: indexs,
                    row: {
                        c_code: rows[i].c_code,
                        c_desc: rows[i].c_desc,
                        c_model: rows[i].c_model,
                        c_unit: rows[i].c_unit,
                        f_price: rows[i].f_pricelast
                    }
                });
                $('#flexigridData').datagrid('selectRow', indexs);
                $('#flexigridData').datagrid('beginEdit', indexs);
            }

        }



        function DoGrid() {
            $('#flexigridData').datagrid({
                iconCls: ' ',
                width: 'auto',
                height: 'auto',
                nowrap: false,
                striped: true,
                fit: true,
                collapsible: true,
                url: '../../ts_lenddetail/GetDataByi_no?i_no=' + $("#Id").val(), //获取数据的url
                sortName: 'Id',
                sortOrder: 'desc',
                idField: 'Id',

                toolbar: [

//                   {
//                       text: '创建',
//                       iconCls: 'icon-add',
//                       handler: function () {
//                           return insert();
//                       }
//                   }
                         ],
                columns: [[
					  { field: 'c_code', title: '编号', width: 90 }
					, { field: 'c_desc', title: '品名', width: 200 }
					, { field: 'c_model', title: '规格', width: 180 }
                    , { field: 'c_unit', title: '单位', width: 65 }
                    , { field: 'f_quant', title: '借出数量', width:70}
                    , { field: 'f_quant_back', title: '归还数量', width: 70, editor: 'text' }
                    , { field: 'c_comment', title: '备注', width: 110, editor: 'text' }  
                      , { field: 'action', title: '操作', width: 80, align: 'center',
                        formatter: function (value, row, index) {
                            if (row.editing) {
                                var s = '<a href="#" onclick="saverow(' + index + ')">保存</a> ';
                                var c = '<a href="#" onclick="cancelrow(' + index + ')">取消</a>';
                                return s + c;
                            } else {
                                var e = '<a href="#" onclick="editrow(' + index + ')">归还</a> ';
                                var d = '<a href="#" onclick="deleterow(' + index + ')">删除</a>';
                                return e + d;
                            }
                        }
                    } 
                ]],
                onBeforeEdit: function (index, row) {
                    row.editing = true;
                    updateActions();
                },
                onAfterEdit: function (index, row) {
                    row.editing = false;
                    updateActions();
                },
                onCancelEdit: function (index, row) {
                    row.editing = false;
                    updateActions();
                },

                pagination: true,
                rownumbers: false

            });

        }

        function updateActions() {
            var rowcount = $('#flexigridData').datagrid('getRows').length;
            for (var i = 0; i < rowcount; i++) {
                $('#flexigridData').datagrid('updateRow', {
                    index: i,
                    row: { action: '' }
                });
            }
        }

        //保存行
        function saverow(index) {
            $('#flexigridData').datagrid('endEdit', index);
            var editrow = $('#flexigridData').datagrid('getRows');
            var row1 = JSON.stringify(editrow[index]);
            var row2 = JSON.parse(row1);
            if (row2.f_quant < row2.f_quant_back) {
                alert("归还数量不能大于借出数量！");
                $('#flexigridData').datagrid('beginEdit', index);
            }
            else if (row2.f_quant == row2.f_quant_back) {
                $('#i_sign').val("全部归还");
            }
            else {
                if ($('#i_sign').val() != "全部归还") {
                    $('#i_sign').val("部分归还");
                } 
            }
        }

        //编辑
        function editrow(index) { 
             $('#flexigridData').datagrid('beginEdit', index);
        }

        function deleterow(index) {
            $.messager.confirm('操作提示', '确定删除吗?', function (r) {
                if (r) {
                    $('#flexigridData').datagrid('deleteRow', index);
                }
            });
        }

        //取消
        function cancelrow(index) {
            $('#flexigridData').datagrid('cancelEdit', index);
        }

        function insert() {

            $('#divQuery').dialog("open");
        }


    </script>
</asp:Content>

